/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.loan;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import src.domain.loan.Loan;
import src.domain.loan.Loansimulate;
import src.util.Constanta;

/**
 *
 * @author jasuke
 */
public class LoansimulateModel implements java.io.Serializable {

    public LoansimulateModel() {
    }
        
    private int getInterval(Date startDate, Date endDate) {
        
        Calendar calFrom = Calendar.getInstance();
        calFrom.setTime(startDate);
        
        Calendar calTo = Calendar.getInstance();
        calTo.setTime(endDate);
                
        int fromYear = calFrom.get(Calendar.YEAR);
        int fromMonth = calFrom.get(Calendar.MONTH);
                
        int toYear = calTo.get(Calendar.YEAR);
        int toMonth = calTo.get(Calendar.MONTH);
        
        int month =  (((toYear-fromYear)*12) - (toMonth - fromMonth));
        
        return Math.abs(month);
    } 
      
    
    public Date setMoth(Date date, int monthIdx) {
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, monthIdx);
        
        return cal.getTime();
    } 
                
    public List<Loansimulate> simulation(Loan loan){
        
        List<Loansimulate> loansimulates = new ArrayList<>();
        loansimulates.clear();
       // belum diinit;
        BigDecimal saldoAwal = loan.getPrincipalAmount();
        BigDecimal loanIntRate = loan.getInterestRate().divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_DOWN);
                
        int interval = loan.getPeriod();//getInterval(loan.getDisbursementDate(), loan.getDueDate());
        
        //v_n:= v_b/12
        BigDecimal n = new BigDecimal(interval).divide(BigDecimal.valueOf(12), 4, RoundingMode.HALF_DOWN);
        
        BigDecimal principalAmount = loan.getPrincipalAmount().divide(BigDecimal.valueOf(interval), 0, RoundingMode.HALF_DOWN);
         BigDecimal outstanding = loan.getPrincipalAmount();   
        BigDecimal outstandingInterest = BigDecimal.ZERO; 
        
        //hitung outstandingBunga
         switch (loan.getInterestType()) {
             case Constanta.INTTYPE_FLAT: 
                    outstandingInterest = loan.getPrincipalAmount().multiply(loanIntRate).multiply(n);
             break;
             case Constanta.INTTYPE_EFFECTIVE: 
                    
             break;
                 
         }
        for (int i=1; i<= interval; i++) {
        
            BigDecimal intTotal;// = BigDecimal.ZERO;
            BigDecimal interestAmount = BigDecimal.ZERO;
            BigDecimal amount = BigDecimal.ZERO;
            
            Date date = setMoth(loan.getDisbursementDate(), i);
            
            switch (loan.getInterestType()) {
                case Constanta.INTTYPE_FLAT:
                     intTotal = loan.getPrincipalAmount().multiply(loanIntRate).multiply(n);
                   
                    interestAmount = intTotal.divide(BigDecimal.valueOf(interval), 0, RoundingMode.HALF_DOWN);
                    amount = loan.getPrincipalAmount().add(intTotal).divide(BigDecimal.valueOf(interval), 0, RoundingMode.HALF_DOWN);
                    if (i==interval){
                        principalAmount = outstanding;
                        interestAmount = outstandingInterest;
                        amount = principalAmount.add(interestAmount);                        
                        outstanding = BigDecimal.ZERO;
                        outstandingInterest = BigDecimal.ZERO;
                    }
                    else {
                        outstanding =  outstanding.subtract(principalAmount);
                        outstandingInterest = outstandingInterest.subtract(interestAmount);
                    }
                    
                   
                    break;

                case Constanta.INTTYPE_EFFECTIVE: 
                    BigDecimal x = saldoAwal.multiply(loanIntRate);
                    interestAmount = x.divide(BigDecimal.valueOf(12), 0, RoundingMode.HALF_DOWN);
                    
                    
                    saldoAwal = saldoAwal.subtract(principalAmount);
                    
                    if (i==interval){
                        principalAmount = outstanding; outstanding = BigDecimal.ZERO;
                     
                    }    
                    else 
                       outstanding = outstanding.subtract(principalAmount); 
                    amount = principalAmount.add(interestAmount);
                    break;
            }
            
            Loansimulate loansimulate = new Loansimulate();
            loansimulate.setPaymentIdx(i);
            loansimulate.setAmount(principalAmount);
            loansimulate.setInterest(interestAmount);
            loansimulate.setTotal(amount);
            loansimulate.setDate(date);
            loansimulate.setOutstanding(outstanding);
            loansimulate.setOutstandingInterest(outstandingInterest);
            loansimulate.setDescription("Angsuran ke "+i);
            
            loansimulates.add(loansimulate);
        }
        
        return loansimulates;
    }
    
    
    public void tess() {
      BigDecimal a = new BigDecimal(0.06);
          
      BigDecimal c = a.divide(BigDecimal.valueOf(24), 4, RoundingMode.HALF_DOWN);
      
      System.out.println(c);
      
      
      
    } 
}
